//
//  CatalogViewController.m
//  Reader
//
//  Created by Jerry Lee on 9/30/09.
//  Copyright 2009 Liox. All rights reserved.
//

#import "CatalogViewController.h"
#import "ReaderAppDelegate.h"
#import "Chapter.h"

@implementation CatalogViewController
@synthesize contentViewController;

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	self.title = @"目 录";
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}

#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    ReaderAppDelegate *appDelegate = (ReaderAppDelegate *)[[UIApplication sharedApplication] delegate];
	return appDelegate.chapters.count;
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }
    
	// Set up the cell
    ReaderAppDelegate *appDelegate = (ReaderAppDelegate *)[[UIApplication sharedApplication] delegate];
	Chapter *chapter= (Chapter *)[appDelegate.chapters objectAtIndex:indexPath.row];
	
	[cell setText:chapter.title];
	return cell;
}




// Override to support row selection in the table view.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	
    // Navigation logic may go here -- for example, create and push another view controller.
	// AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherView" bundle:nil];
	// [anotherViewController release];	
	
	ReaderAppDelegate *delegate = (ReaderAppDelegate *)[[UIApplication sharedApplication] delegate];
	[delegate.window makeKeyAndVisible];
	
	
	//ReaderAppDelegate *appDelegate = (ReaderAppDelegate *)[[UIApplication sharedApplication] delegate];
	Chapter *chapter = (Chapter *)[delegate.chapters objectAtIndex:indexPath.row];
	
	if(self.contentViewController == nil) {
		ContentViewController *viewController = [[ContentViewController alloc] initWithNibName:@"ContentViewController" bundle:nil];
		self.contentViewController = viewController;
		[viewController release];
	}
	
	// Setup the chapter
	[self.navigationController pushViewController:self.contentViewController animated:YES];	
	self.contentViewController.title = [chapter title];
	NSString *imagePath = [[NSBundle mainBundle] resourcePath];
	imagePath = [imagePath stringByReplacingOccurrencesOfString:@"/" withString:@"//"];
	imagePath = [imagePath stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
	NSString *HTMLData = [chapter content];
	[self.contentViewController.bookContent loadHTMLString:HTMLData baseURL:[NSURL URLWithString: [NSString stringWithFormat:@"file:/%@//", imagePath]]];
	
}


- (void)dealloc {
	[contentViewController release];
    [super dealloc];
}


@end
